<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<div class="easyui-panel" title="Nested Panel"
	data-options="width:'100%',minHeight:500,noheader:true,border:false"
	style="padding:10px;">
	<div class="easyui-layout" style="height:240px;">
		<div data-options="region:'west',split:false"
			style="width:600px;padding:5px">
			<ul id="needCategory" class="easyui-tree">
			</ul>
		</div>
		<div data-options="region:'center'" style="padding:5px">
			<div id="need_detail">
				<div></div>
			</div>
		</div>
	</div>
	<div class="easyui-layout" style="height:240px;">
		<div data-options="region:'west',split:false"
			style="width:600px;padding:5px">
			<ul id="testCategory" class="easyui-tree"
				data-options="url:'/test/category/list',animate: true,method : 'GET'">
			</ul>
		</div>
		<div data-options="region:'center'" style="padding:5px">
			<div id="test_detail">
				<div></div>
			</div>
		</div>
	</div>
	<div>
		<div data-options="region:'center'" style="padding:5px">
			<table class="easyui-datagrid" id="mapping">
			</table>
		</div>
	</div>
	<shiro:hasRole name="admin">
		<div id="contentCategoryMenu" class="easyui-menu" style="width:120px;"
			data-options="onClick:menuHandler">
			<div data-options="iconCls:'icon-add',name:'add'">添加</div>
			<div data-options="iconCls:'icon-remove',name:'rename'">重命名</div>
			<div data-options="iconCls:'icon-edit',name:'edit'">编辑详情</div>
			<div class="menu-sep"></div>
			<div data-options="iconCls:'icon-cancel',name:'delete'">删除</div>
		</div>
	
		<div id="contentCategoryMenu2" class="easyui-menu" style="width:120px;"
			data-options="onClick:menuHandler2">
			<div data-options="iconCls:'icon-add',name:'add'">添加</div>
			<div data-options="iconCls:'icon-remove',name:'rename'">重命名</div>
			<div data-options="iconCls:'icon-edit',name:'edit'">编辑详情</div>
			<div class="menu-sep"></div>
			<div data-options="iconCls:'icon-cancel',name:'delete'">删除</div>
		</div>
	</shiro:hasRole>
</div>
<script type="text/javascript">
$(function(){
	var tree = $("#needCategory");
	var tree2 = $("#testCategory");

	$("#needCategory").tree({
		url : '/need/category/list',
		animate: true,
		method : "GET",
		lines:true,
		onContextMenu: function(e,node){
            e.preventDefault();
            //console.log(node);
            $(this).tree('select',node.target);
            //console.log(node);
            $('#contentCategoryMenu').menu('show',{
                left: e.pageX,
                top: e.pageY
            });
        },
        onAfterEdit : function(node){
        	var _tree = $(this);
        	console.log('isAdd: ' + node.isAdd);
        	if(node.isAdd==1) {
        		//新建的node的id默认是0，因此当id是0时
	       		// 新增节点
	       		$.post("/need/category/create",{parentId:node.parentId,name:node.text,isPon:node.isPon},function(data){
	       			if(data.status == 200){
	       				_tree.tree("update",{
	           				target : node.target,
	           				id : data.data.id,
	           				text:data.data.text,
	           				isParent:data.data.isParent,
	           				isPon:data.data.isPon
	           			});
	           			
	       			}else{
	       				$.messager.alert('提示','创建'+node.text+' 失败!');
	       			}
	       		});
        	} else {
        		$.post("/need/category/update",{id:node.id,parentId:node.parentId,name:node.text,designNo:node.designNo,isParent:node.isParent},function(data){
	       			if(data.status == 200){
	       				_tree.tree("update",{
	           				target : node.target,
	           				id : data.data.id,
	           				text:data.data.name,
	           				isParent:data.data.isParent
	           			});
	           			$.ajax({
							url:'/need/querybyno',
							dataType:'html',
							data:{
								id:node.id,
								designNo:node.designNo,
								isParent:node.isParent
							},
							success:function(data) {
								$('#need_detail').empty();
								$('#need_detail').append(data);
							}
						})
	       			}else{
	       				$.messager.alert('提示','创建'+node.text+' 失败!');
	       			}
	       		});
        		
        	}
        	
        },
        onClick : function(node){
			if(tree.tree("isLeaf",node.target)){
				var no = tree.tree("getSelected").designNo;
				var pid = tree.tree("getSelected").parentId;
				var name = tree.tree("getSelected").text;
				var isParent = tree.tree("getSelected").isParent;
				var id = tree.tree("getSelected").id;
				var isPon = tree.tree("getSelected").isPon;
				//console.log('isPon: '+ isPon);
				$.ajax({
					url:'/need/querybyno',
					data:{
						designNo:no,
						name:name,
						id:id,
						isParent:isParent,
						isPon:isPon
						},
					dataType:'html',
					success:function(data) {
						$('#need_detail').empty();
						//console.log(data);
						$('#need_detail').append(data);
					}
					
				
				})
			}
		}
	});
	
	tree2.tree({
		url : '/test/category/list',
		animate: true,
		method : "GET",
		lines:true,
		onContextMenu: function(e,node){
            e.preventDefault();
            //console.log(node);
            $(this).tree('select',node.target);
            //console.log(node);
            $('#contentCategoryMenu2').menu('show',{
                left: e.pageX,
                top: e.pageY
            });
        },
        onAfterEdit : function(node){
        	var _tree = $(this);
        	
        	//如果是添加节点
        	if(node.isAdd==1) {
	       		// 新增节点
	       		$.post("/test/category/create",{parentId:node.parentId,name:node.text},function(data){
	       			if(data.status == 200){
	       				_tree.tree("update",{
	           				target : node.target,
	           				id : data.data.id,
	           				text:data.data.name,
	           				isParent:data.data.isParent
	           			});
	       			}else{
	       				$.messager.alert('提示','创建'+node.text+' 节点失败!');
	       			}
	       		});
        	} else {
        		$.post("/test/category/update",{id:node.id,parentId:node.parentId,name:node.text,caseNo:node.caseNo,isParent:node.isParent},function(data){
	       			if(data.status == 200){
	       			
	       			
	       			_tree.tree("update",{
	           				target : node.target,
	           				id : data.data.id,
	           				text:data.data.name,
	           				isParent:data.data.isParent
	           			});
	           			$.ajax({
							url:'/test/querybyno',
							dataType:'html',
							data:{
								id:node.id,
								caseNo:node.caseNo,
								isParent:node.isParent
							},
							success:function(data) {
								$('#test_detail').empty();
								$('#test_detail').append(data);
							}
						})
	       			
	       			
	       			}else{
	       				$.messager.alert('提示','创建'+node.text+' 节点失败!');
	       			}
	       		});
        	}
        	
        	
        	
        },
		onClick : function(node){
			if(tree2.tree("isLeaf",node.target)){
				var no = tree2.tree("getSelected").caseNo;
				//console.log('no: '+ no);
				var pid = tree2.tree("getSelected").parentId;
				var id = tree2.tree("getSelected").id;
				var isParent = tree2.tree("getSelected").isParent;
				$.ajax({
					url:'/test/querybyno',
					data:{
						caseNo:no,
						id:id,
						isParent:isParent
						},
					dataType:'html',
					success:function(data) {
						$('#test_detail').empty();
						//console.log(data);
						$('#test_detail').append(data);
					}
					
				
				})
			}
		}
	});
	
	
	
	$('#mapping').datagrid({
		url:'/map/listonehour',
		toolbar:[{
			iconCls: 'icon-add',
			handler:function(){
			    var tree = $("#needCategory");
				var tree2 = $("#testCategory");
		    	if(tree.tree("getSelected")==null || tree.tree("getSelected").designNo==null||tree.tree("getSelected").designNo=='') {
		    		$.messager.alert('提示','请选中一条设计需求！');
		    		return;
		    	}
		    	if(tree2.tree("getSelected")==null || tree2.tree("getSelected").caseNo==null||tree2.tree("getSelected").caseNo=='') {
		    		$.messager.alert('提示','请选中一条测试用例！！');
		    		return;
		    	}
		    	var no1 = tree.tree("getSelected").designNo;
		    	var designText = tree.tree("getSelected").text;
		    	var no2 = tree2.tree("getSelected").caseNo;
		    	var caseText = tree2.tree("getSelected").text;
		    	
		    	//建立映射之前需要判断一下是否之前已经建立过这一对映射，如果已经建立了，就提示不能重复建立
		    	var params = {"designNo":no1,"designText":designText,"caseText":caseText,"caseNo":no2};
		    	$.get("/map/isbuild",params, function(data){
		    		if(data.data==null) {
		    			//console.log("data==null");
		    			$.post("/map/create",params,function(data){
				    		if(data.status == 200) {
				    			$("#mapping").datagrid("reload");
				    		}
			    		});
		    		} else {
		    			$.messager.alert('提示', '此对映射关系已经建立，不能重复建立!');
		    		}
		    	});
		    	
		    },
			text:'建立映射'
		},'-',{
			iconCls: 'icon-cancel',
			handler: function(){
				var ids = TT.getSelectionsIds("#mapping");
		    	if(ids.length == 0){
		    		$.messager.alert('提示','未选中行!');
		    		return ;
		    	}
		    	$.messager.confirm('确认','确定删除吗？',function(r){
		    	    if (r){
		    	    	var params = {"ids":ids};
		            	$.post("/map/delete",params, function(data){
		        			if(data.status == 200){
		        				$.messager.alert('提示','删除成功!',undefined,function(){
		        					$("#mapping").datagrid("reload");
		        				});
		        			}
		        		});
		    	    }
		    	});
			},
			text:'删除映射'
		},'-',{
			iconCls: 'icon-search',
			handler: function(){
				var ids = TT.getSelectionsIds("#mapping");
				var strs= new Array();
				var split =ids.split(",");
				for (var i in split) {
					strs.push(split[i]);
				}
				if(strs[0]==null || strs[0]==""){
					$.messager.alert('提示','未选中任何映射!');
					return;
				} else {
					if(strs[1]==null || strs[1]==""){
						TAOTAO.createWindow({
	        				url : "/map/detail?id="+ids,
	        			});
					} else {
						$.messager.alert('提示','只能选择一条!');
	        			return ;
					}
				}
			},
			text:'查看详情'
		}],
		striped:true,
		singleSelect:false,
		collapsible:true,
		pagination:true,
		method:'get',
		pageSize:10,
		columns:[[
			{field:'ck',checkbox:true },
			{field:'caseNo',width:80,title:'测试用例编号'},
			{field:'caseText',width:500,title:'测试用例'},
			{field:'designNo',width:80,title:'需求编号'},
			{field:'designText',width:300,title:'设计需求'},
			{field:'createdTime',width:130,title:'建立时间',
				formatter:TAOTAO.formatDateTime
			}
		]]
	
	})
});


function menuHandler(item){
	var tree = $("#needCategory");
	//var tree2 = $("#testCategory");
	var node = tree.tree("getSelected");
	//var node2 = tree2.tree("getSelected");
	//console.log(node.type);
	//如果选中的不是测试用例树
	if(item.name === "add"){
		
		//console.log('designNo :'+node.designNo);
		if (!tree.tree("isLeaf",node.target) || node.designNo==null) {
			tree.tree('append', {
	            parent: (node?node.target:null),
	            data: [{
	                text: '新建分类',
	                id : 0,
	                parentId : node.id,
	                isPon : node.isPon
	            }]
	        }); 
			var _node = tree.tree('find',0);
			_node.isAdd=1;
			tree.tree("select",_node.target).tree('beginEdit',_node.target);
		} else {
			
		}
		
	}else if(item.name === "rename"){
		node.isAdd=0;
		tree.tree('beginEdit',node.target);
	}else if(item.name === "delete"){
		$.messager.confirm('确认','确定删除名为 '+node.text+' 的分类吗？',function(r){
			if(r){
				$.post("/need/category/delete/",{parentId:node.parentId,id:node.id,designNo:node.designNo},function(data){
					if(data.status==200) {
						tree.tree("remove",node.target);
						$("#mapping").datagrid("reload");
					} else {
						$.messager.alert('提示','删除失败！');
					}
					
				});
				
			}
		});
	} else if(item.name === "edit"){
		if (!tree.tree("isLeaf",node.target) || node.designNo==null) {
			
		} else {
			var no = node.designNo;
			$.ajax({
				url:'/need/editpage',
				dataType:'html',
				data:{designNo:no},
				success:function(data){
					$('#need_detail').empty();
					//console.log(data);
					$('#need_detail').append(data);
				}
			})
		}
	}
}

function menuHandler2(item){
	var tree = $("#testCategory");
	//var tree2 = $("#testCategory");
	var node = tree.tree("getSelected");
	//var node2 = tree2.tree("getSelected");
	//console.log(node.type);
	//如果选中的不是测试用例树
	if(item.name === "add"){
		
		//console.log('designNo :'+node.designNo);
		if (!tree.tree("isLeaf",node.target) || node.caseNo==null) {
			tree.tree('append', {
	            parent: (node?node.target:null),
	            data: [{
	                text: '新建分类',
	                id : 0,
	                parentId : node.id
	            }]
	        });
			var _node = tree.tree('find',0);
	        _node.isAdd=1;
			tree.tree("select",_node.target).tree('beginEdit',_node.target);
		} else {
			
		}
		
	}else if(item.name === "rename"){
		node.isAdd=0;
		tree.tree('beginEdit',node.target);
	}else if(item.name === "delete"){
		$.messager.confirm('确认','确定删除名为 '+node.text+' 的节点吗？',function(r){
			if(r){
				$.post("/test/category/delete/",{parentId:node.parentId,id:node.id,caseNo:node.caseNo},function(data){
					if(data.status==200) {
						tree.tree("remove",node.target);
						$("#mapping").datagrid("reload");
					} else {
						$.messager.alert('提示','删除失败！');
					}
				});
				
			}
		});
	}else if(item.name === "edit"){
		if (!tree.tree("isLeaf",node.target) || node.caseNo==null) {
			
		} else {
			var no = node.caseNo;
			$.ajax({
				url:'/test/editpage',
				dataType:'html',
				data:{caseNo:no},
				success:function(data){
					$('#test_detail').empty();
					//console.log(data);
					$('#test_detail').append(data);
				}
			})
		}
	}
}


</script>